//
//  LWNewProjectCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/5/10.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWNewProjectCtrl.h"
#import "AddPersonInfoView.h"
#import "CustomSpannerView.h"
#import "HomeDataTool.h"
#import "SelectImgUtils.h"
#

@interface LWNewProjectCtrl ()<CustomSpannerViewDelegate,UITextViewDelegate,SelectImgUtilsDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITextView *projDescTextView;

@property (weak, nonatomic) IBOutlet UILabel *projTypeLabel;

@property (weak, nonatomic) IBOutlet UILabel *singStatusLabel;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;


@property (weak, nonatomic) IBOutlet UITextField *gcmcField;
@property (weak, nonatomic) IBOutlet UITextField *xmddField;
@property (weak, nonatomic) IBOutlet UITextField *jzmjField;
@property (weak, nonatomic) IBOutlet UITextField *kgrqField;
@property (weak, nonatomic) IBOutlet UITextField *jsfsField;
@property (weak, nonatomic) IBOutlet UITextField *ryqkField;




@property (weak, nonatomic) IBOutlet UIView *ryqkView;
@property (weak, nonatomic) IBOutlet UIView *ryqkRightVeiw;
@property (weak, nonatomic) IBOutlet UIView *ryqkContainsView;


@property (nonatomic,strong) NSMutableArray *ryqkViewArray;

@property (nonatomic,weak)UIView *currentEditView;
@property (nonatomic,assign)CGFloat moveDis;//移动距离
@property (nonatomic,assign)CGFloat keyboardHeight;//键盘高度，如果为0，代表当前不是编辑模式
@property (weak, nonatomic) IBOutlet UIImageView *projectImg;
@property (weak, nonatomic) IBOutlet UILabel *selectImgLabel;


@property (nonatomic,copy)NSString *projectImgUrl;
@end

@implementation LWNewProjectCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"工程信息发布"];
    
    self.ryqkViewArray = [NSMutableArray array];
    
    [self.projDescTextView setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
    
    
    [self.gcmcField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.xmddField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.jzmjField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.kgrqField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.jsfsField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [self.ryqkField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    
    [self.projDescTextView setDelegate:self];
    
    //添加键盘弹出与隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.scrollView setContentSize:CGSizeMake(SCREENW, CGRectGetMaxY(self.bottomView.frame)+15)];
}

- (void)textFieldBeginEditing:(UITextField *)textField{
    NSLog(@"textViewShouldBeginEditing");
    self.currentEditView = textField;
    if (self.keyboardHeight!=0) {
        [self adjustLocationWithKeyboardHeight:self.keyboardHeight];
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    NSLog(@"textViewShouldBeginEditing");
    self.currentEditView = textView;
    if (self.keyboardHeight!=0) {
        [self adjustLocationWithKeyboardHeight:self.keyboardHeight];
    }
    return YES;
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //在键盘显示的时候去调整view
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    self.keyboardHeight = keyboardSize.height;
    [self adjustLocationWithKeyboardHeight:keyboardSize.height];
}

-(void)adjustLocationWithKeyboardHeight:(CGFloat)keyboardHeight{
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect editViewRect = [self.currentEditView convertRect:self.currentEditView.bounds toView:window];
    
    //首页判断键盘有没有遮挡view，如果没有遮挡，那个什么也不做
    CGFloat viewMaxY = CGRectGetMaxY(editViewRect);
    if (viewMaxY+10>SCREENH-keyboardHeight) {
        //代表键盘遮挡了view
        //需要调整uiscrollview的contentoffset移动
        self.moveDis = (viewMaxY+10)-(SCREENH-keyboardHeight);
        [UIView animateWithDuration:0.25 animations:^{
            [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y+self.moveDis)];
        }];
    }
}

-(void)keyboardWillHide:(NSNotification*)notif{
    self.keyboardHeight = 0;
    self.currentEditView = nil;
    if (self.scrollView.contentOffset.y!=0) {
        [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y-self.moveDis)];
    }
    self.moveDis = 0;
}


- (IBAction)addPerson:(id)sender {
    CGFloat insetY = 0;
    if ([self.ryqkViewArray lastObject]) {
        UIView *tempView = [self.ryqkViewArray lastObject];
        insetY = CGRectGetMaxY(tempView.frame)+8;
    }
    
    AddPersonInfoView *infoView = [[AddPersonInfoView alloc] initWithFrame:CGRectMake(0, insetY, self.ryqkContainsView.width, 25)];
    [infoView.textField addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [infoView.deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.ryqkViewArray addObject:infoView];
    [self.ryqkContainsView addSubview:infoView];
    [self.ryqkContainsView setHeight:CGRectGetMaxY(infoView.frame)];
    [self.bottomView setY:CGRectGetMaxY(self.ryqkContainsView.frame)+8];
    [self.scrollView setContentSize:CGSizeMake(SCREENW, CGRectGetMaxY(self.bottomView.frame)+15)];
}

-(void)deleteBtnClick:(UIButton *)btn{
    UIView *view = [btn superview];
    [self.ryqkViewArray removeObject:view];
    [view removeFromSuperview];
    if (self.ryqkViewArray.count) {
        for (int i=0; i<self.ryqkViewArray.count; i++) {
            UIView *view = [self.ryqkViewArray objectAtIndex:i];
            [view setY:i*25+i*8];
            [self.ryqkContainsView setHeight:CGRectGetMaxY(view.frame)];
            [self.bottomView setY:CGRectGetMaxY(self.ryqkContainsView.frame)+8];
            [self.scrollView setContentSize:CGSizeMake(SCREENW, CGRectGetMaxY(self.bottomView.frame)+15)];
        }
    }else{
        [self.ryqkContainsView setHeight:0];
        [self.bottomView setY:CGRectGetMaxY(self.ryqkContainsView.frame)+8];
        [self.scrollView setContentSize:CGSizeMake(SCREENW, CGRectGetMaxY(self.bottomView.frame)+15)];
    }

}

- (IBAction)enterBtnClick:(id)sender {
    if (self.projectImg.image) {
        SelectImgUtils *utils = [SelectImgUtils shareInstance];
        [utils setDelegate:self];
        [utils uploadImg:self.projectImg.image];
    }else{
        [self uploadProject];
    }
}

-(void)uploadProject{
    if (ISBLANK(self.gcmcField.text) ||ISBLANK(self.xmddField.text) ||ISBLANK(self.jzmjField.text) ||ISBLANK(self.kgrqField.text) ||ISBLANK(self.jsfsField.text) ||ISBLANK(self.ryqkField.text)) {
        [DialogUtils showTipsWithView:nil String:@"请将数据填写完整"];
        return;
    }
    
    for (AddPersonInfoView *view in self.ryqkViewArray) {
        if (ISBLANK(view.textField.text)) {
            [DialogUtils showTipsWithView:nil String:@"请将数据填写完整"];
            return;
        }
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.gcmcField.text forKey:@"title"];
    [dic setObject:self.projDescTextView.text forKey:@"description"];
    if (self.projectImgUrl) {
        [dic setObject:self.projectImgUrl forKey:@"picPaths"];
    }
    [dic setObject:[self.projTypeLabel.text isEqualToString:@"认证工程"]?@(1):@(2) forKey:@"type"];
    [dic setObject:self.singStatusLabel.text forKey:@"attr_signStatus"];
    [dic setObject:self.xmddField.text forKey:@"attr_projectAddress"];
    [dic setObject:self.jzmjField.text forKey:@"attr_buildArea"];
    [dic setObject:self.kgrqField.text forKey:@"attr_openDate"];
    
    NSMutableString *ryyq = [NSMutableString stringWithString:self.ryqkField.text];
    for (AddPersonInfoView *view in self.ryqkViewArray) {
        [ryyq appendFormat:@";%@",view.textField.text];
    }
    [dic setObject:ryyq forKey:@"attr_laborRequirements"];//人员要求
    [dic setObject:self.jsfsField.text forKey:@"attr_settlementRequirements"];
    
    [HomeDataTool uploadProjectWithParam:dic success:^(BaseResponse *result) {
        if (result.isSucc) {
            [DialogUtils showTipsWithView:nil String:@"发布成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:nil String:@"工程信息发布失败"];
    }];
}



- (IBAction)statusBtnClick:(id)sender {
    UIButton *btn = sender;
    NSArray *stringArray = STRING_ARRAY_PROJECT_TYPE_STATUS;
    if(btn.tag == 1){
        //工程类型
        stringArray = STRING_ARRAY_PROJECT_TYPE_STATUS;
    }else if(btn.tag == 2){
        //签约情况
        stringArray = STRING_SIGN_ARRAY_STATUS;
    }
    CustomSpannerView *spannerView = [[CustomSpannerView alloc] initWithStringArray:stringArray bearingView:btn];
    [spannerView setDelegate:self];
    [spannerView show];
}

-(void)customSpannerItemClick:(NSString *)title index:(NSInteger)index bearingView:(UIView *)bearingView{
    if (bearingView.tag == 1) {
        [self.projTypeLabel setText:title];
    }else if(bearingView.tag == 2){
        [self.projTypeLabel setText:title];
    }
}

- (IBAction)uploadImg:(id)sender {
    SelectImgUtils *utils = [SelectImgUtils shareInstance];
    [utils setDelegate:self];
    [utils selectImageWithCtrl:self];
}

-(void)selectImgUtilsImg:(UIImage *)img{
    [self.projectImg setImage:img];
    [self.selectImgLabel setHidden:YES];
}

-(void)selectImgUtilsUrl:(NSString *)url{
    //上传成功
    self.projectImgUrl = url;
    [self uploadProject];
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}

- (void)dealloc{
    //移除键盘监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end
